xcannon_types = {
	xcannon = {
		name = 'Light xcannon',					-- The name, of course. 
		model = 'models/props_lab/jar01b.mdl',  -- The model that represents the launcher
		power = 1500,                           -- The amount of power the shell is launched with.
		delay = 1.0,                            -- Delay between firings
		sound = Sound( 'Airboat_impact_hard' ), -- Sound used at time of launch
		effect = 'Sparks',                      -- Effect at time of launch
		endeffect = 'Explosion',
		radius = 300,							-- Explosion radius
		damage = 100,							-- Explosion damage
		shellradius = 4,						-- Radius of the shell
		shellgravity = 1.0,						-- Shell gravity. This way you can have slow shells that remain in the air longer, such as the mortar.
		kickback = 1000,						-- Amount of kickback this weapon gives when you fire it.
		traillength = 15						-- The length of the 'motion blur' trail
		},
	hvyxcannon = {
		name = 'Heavy xcannon',
		model = 'models/props_borealis/bluebarrel001.mdl',
		power = 1000,
		delay = 2.0,
		sound = Sound( 'Airboat.FireGunHeavy' ),
		effect = 'HelicopterMegaBomb',
		endeffect = 'Explosion',
		radius = 600,
		damage = 200,
		shellradius = 16,
		shellgravity = 0.4,
		kickback = 3000,
		traillength = 15
		},
	Artillery = {
		name = 'Artillery',
		model = 'models/props_wasteland/laundry_basket001.mdl',
		power = 6000,
		delay = 10.0,
		sound = Sound( 'Airboat.FireGunHeavy' ),
		effect = 'HelicopterMegaBomb',
		endeffect = 'Explosion',
		radius = 800,
		damage = 400,
		shellradius = 21,
		shellgravity = 0.6,
		kickback = 10000,
		traillength = 25
		},
	flak = {
		name = 'Flak xcannon',
		model = 'models/props_c17/chair_stool01a.mdl',
		power = 1000,
		delay = 0.5,
		sound = Sound( 'Airboat.FireGunHeavy' ),
		effect = 'Sparks',
		endeffect = 'HelicopterMegaBomb',
		radius = 1000,
		damage = 10,
		shellradius = 2,
		shellgravity = 0.1,
		traillength = 15,
		kickback = 50
	},
	railgun = {
		name = 'Railgun',
		model = 'models/props_junk/TrafficCone001a.mdl',
		power = 30000,
		kickback = 3000,
		delay = 4.0,
		sound = Sound( 'Airboat.FireGunHeavy' ),
		effect = 'Sparks',
		endeffect = 'Explosion',
		radius = 50,
		damage = 1000,
		shellradius = 2,
		shellgravity = 0.0,
		traillength = 150
	},
	bombdropperheavy = {
		name = 'Heavy Bomb Bay',
		model = 'models/props_junk/MetalBucket01a.mdl',
		power = 0,
		kickback = 1,
		delay = 2.0,
		sound = Sound( 'Airboat.FireGunHeavy' ),
		effect = 'none',
		endeffect = 'Explosion',
		radius = 200,
		damage = 130,
		shellradius = 10,
		shellgravity = 1.8,
		traillength = 15
	},
	bombdropperlight = {
		name = 'Light Bomb Bay',
		model = 'models/props_junk/MetalBucket01a.mdl',
		power = 0,
		kickback = 1,
		delay = 1.0,
		sound = Sound( 'Airboat.FireGunHeavy' ),
		effect = 'none',
		endeffect = 'Explosion',
		radius = 100,
		damage = 50,
		shellradius = 8,
		shellgravity = 1.0,
		traillength = 15
	},
	machinegun = {
		name = 'Machine Gun',
		model = 'models/props_junk/TrafficCone001a.mdl',
		power = 1500,
		kickback = 100,
		delay = 0.05,
		sound = Sound( 'Airboat.FireGunHeavy' ),
		effect = 'Sparks',
		endeffect = 'StunstickImpact',
		offset = Angle(90, 0, 0),
		bottom = 'z',
		radius = 50,
		damage = 5,
		shellradius = 1,
		shellgravity = 0.2,
		traillength = 15,
		shellmass = 1
	}
}


TOOL.Category = "(Malawar)"
TOOL.Name = "#xcannon"
TOOL.Command = nil
TOOL.ConfigName = ""

TOOL.ClientConVar['button'] = '1'
TOOL.ClientConVar['type'] = 'xcannon'
TOOL.ClientConVar['selfdestruct'] = '15.0'

cleanup.Register('xcannons')

if ( CLIENT ) then

	language.Add( "Tool_xcannon_name", "xcannon Spawner (Malawar)" )
	language.Add( "Tool_xcannon_desc", "Create ballistic xcannons attached to any surface!" )
	language.Add( "Tool_xcannon_0", "Click somewhere to spawn a xcannon." )

	language.Add( "Undone_xcannon", "Undone xcannon" )
	language.Add( "Cleanup_xcannon", "xcannon" )
	language.Add( "Cleaned_xcannon", "Cleaned up all xcannons" )
	language.Add( 'SBoxLimit_xcannons', 'Maximum xcannons Reached' )

end


if (SERVER) then
  CreateConVar('sbox_maxxcannons',5)

end

	
function TOOL:LeftClick( trace )
	if(CLIENT) then
		return true
	end

	if(!SERVER) then return false end
	
	if trace.Entity && trace.Entity:IsPlayer() then return false end

	local ply = self:GetOwner()
	
	if ( !self:GetSWEP():CheckLimit( "xcannons" ) ) then return false end

	
	local ctype = self:GetClientInfo('type') or 'xcannon'
	local cdata = xcannon_types[ctype]
	
	if !cdata then return end
	

	local ent = ents.Create( 'malawar_xcannon' )
		
		ent:SetModel(cdata.model)
		ent:SetVar('xcannonType', ctype)
		
		ent:SetPos( trace.HitPos - trace.HitNormal * ent:OBBMins().z)
		local Ang = trace.HitNormal:Angle()
	
		Ang.pitch = Ang.pitch + 90
		
		ent:SetAngles(Ang)
		ent:SetVar('Owner',ply)
	ent:Spawn()
	
	ent:SetVar('selfdestruct', self:GetClientNumber('selfdestruct'))
	
	local phys = ent:GetPhysicsObject()	
	if (phys:IsValid()) then
		phys:Wake()
	end
	
	phys:SetMass(5)
	
	if ( trace.Entity:IsValid() ) then
		local const = constraint.Weld(ent, trace.Entity,0, trace.PhysicsBone, 0, systemmanager )
		nocollide = constraint.NoCollide( ent, trace.Entity, 0, trace.PhysicsBone )
		trace.Entity:DeleteOnRemove( ent )
	end
	
	local key = self:GetClientNumber('button')
	
	undo.Create('xcannon')
		undo.AddEntity( ent )
		undo.AddEntity( const )
		undo.AddEntity( nocollide )

		undo.SetPlayer( ply )
	undo.Finish()
	
	numpad.OnDown(ply, key, 'xcannon_On', ent)
	numpad.OnUp(ply, key, 'xcannon_Off', ent)
	
	ply:AddCleanup( 'xcannons', ent)
	ply:AddCleanup( 'xcannons', const )
	ply:AddCleanup( 'xcannons', nocollide )
	
	ply:AddCount('xcannons',ent)

	return true
end

function TOOL.BuildCPanel( cp )
	cp:AddControl( "Header", { Text = "#Tool_xcannon_name", Description	= "#Tool_xcannon_desc" }  )
	
	local combo = {}
	combo.Label = 'xcannon Type'
	combo.MenuButton = 1
	combo.Folder = 'settings/menu/main/construct/xcannon/'
	combo.Options = {}
	
	for key,weapon in pairs(xcannon_types) do
		combo.Options[weapon.name] = { xcannon_type = key }
	end
	
	cp:AddControl('ComboBox', combo) 
	cp:AddControl( 'Slider', { Label = 'Self-destruct Delay', Type = "Float", Min = 0.1, Max = 30.0, Command = 'xcannon_selfdestruct' } )
	cp:AddControl('Numpad', { Label = 'Fire:', ButtonSize = '22', Command = 'xcannon_button' } )
end


function UpdateGhostxcannon( ent, player )
	--Msg('THINK1\n')

	if ( !ent ) then return end
	--Msg('THINK2\n')
	if ( !ent:IsValid() ) then return end
	--Msg('THINK3\n')

	local tr 	= utilx.GetPlayerTrace( player, player:GetCursorAimVector() )
	local trace 	= util.TraceLine( tr )
	if (!trace.Hit) then return end
	--Msg('THINK4\n')

	if (trace.Entity && trace.Entity:GetClass() == "malawar_xcannon" || trace.Entity:IsPlayer() || trace.Entity:IsNPC()) then
	
		ent:SetNoDraw( true )
		return
		
	end
	
	local Ang = trace.HitNormal:Angle()
	Ang.pitch = Ang.pitch + 90
	
	local min = ent:OBBMins()
	ent:SetPos( trace.HitPos - trace.HitNormal * min.z )
	ent:SetAngles( Ang )
	
	ent:SetNoDraw( false )
	
	
end


function TOOL:Think()
	local ctype = self:GetClientInfo('type') or 'xcannon'
	local cdata = xcannon_types[ctype]
	
	local model = cdata.model
	
	if (!self.GhostEntity || !self.GhostEntity:IsValid() || self.GhostEntity:GetModel() != model) then
		self:MakeGhostEntity( model, Vector(0,0,0), Angle(0,0,0) )
	end
	
	UpdateGhostxcannon( self.GhostEntity, self:GetOwner() )
end
